/*
 * Customer Action
 */
var CKL     = require("../utils/checklogin"),
Response    = require("../utils/response"),
DaoMysql    = require('../models/dao-mysql').DaoMysql,
CacheRedis  = require('../models/cache-redis').CacheRedis,
User        = require('../models/user').User;
Page        = require('../models/page').Page;

module.exports = function(app)
{
	var dao = new DaoMysql(app.envConfig.dbMysql, app.mysqlClient, app.logmessage, new CacheRedis(app.envConfig.storeRedis, app.redisClient, app.logmessage, app.defs.DEFAULT_CACHE_EXPIRE_SECS));
	
	//客服列表get请求，验证是否登录
	app.get(app.envConfig.routes.customer_user_list.url,CKL.logined);
	app.get(app.envConfig.routes.customer_user_list.url,function(req,res){
		customerList(req,res);
	});
	
	//客服列表post请求，验证是否登录
	app.post(app.envConfig.routes.customer_user_list.url,CKL.logined);
	app.post(app.envConfig.routes.customer_user_list.url,function(req,res){
		customerList(req,res);
	});
	
	//客服查询方法
	function customerList(req,res){
		
		if(!req.body.pageNo){
			req.body.pageNo=0;
		}
		if(!req.body.pageSize){
			req.body.pageSize=5;
		}
		dao.searchPage(User,"role=1",req.body.pageNo+","+req.body.pageSize,function(err,result,total){
			if(err){
				msg(err);
			}
			var page=new Page(req.body.pageNo,req.body.pageSize,result,total);
			Response.render(app,res,app.envConfig.routes.customer_user_list.page,{user:req.session.user,page:page});
		});
	}
	
	//添加客服
	app.post(app.envConfig.routes.customer_user_add.url,CKL.logined);
	app.post(app.envConfig.routes.customer_user_add.url,function(req,res,next){
		addOrUpdateCustomer(req,res,next);
	});
	
	//添加客服
	function addOrUpdateCustomer(req,res,next)
	{
    	//添加操作
		if(!req.body.cusId)
    	{
    		dao.search(User,'username="'+req.body.cusName+'"',function(err,result){
    			if(err || result.length > 0){
    				return Response.jsonerror(app,res,{msg:"客服名称已经存在，请重新输入!"});
    			}
    			else
    			{
    					//获得客服用户名
    					User.username = req.body.cusName;
    					//获得客服密码
    					User.password = req.body.cusPwd;
    					//获得客服真实姓名
    					User.realname = req.body.cusRealName;
    					User.role = 1;
    					
    					dao.create(new User(User), function(err,result){
    						if(err || !result){
    							return Response.jsonerror(app,res,{msg:"添加失败"});
    						}else{
    							return Response.jsonsuccess(app,res,{msg:"添加成功"});
    						}
    					});
    			}
    		});
    	}
    	//修改操作
    	else
    	{
    		dao.get(User,req.body.cusId,function(err,result){
				if(err || !result){
					return Response.jsonerror(app,res,{msg:"无数据"});
				}else{
					dao.search(User,'username="'+req.body.cusName+'"',function(err,result1){
						//如果输入的用户名与数据库用户名不一致，则查询是否存在相同用户名
						if(err || !result1){
							return Response.jsonerror(app,res,{msg:"修改失败"});
						}
						if(result1.length > 0 && result1[0].id!=req.body.cusId){
							return Response.jsonerror(app,res,{msg:"客服名称已经存在，请重新输入!"});
						}
						else
						{
							result.username=req.body.cusName;
							result.password=req.body.cusPwd;
							result.realname=req.body.cusRealName;
							dao.update(new User(result), function(err,result){
								if(err || !result){
									return Response.jsonerror(app,res,{msg:"修改失败"});
								}else{
									return Response.jsonsuccess(app,res,{msg:"修改成功"});
								}
							});
						}
					});
					
				}
			});
    	}
    	
	}
	
	//客服id查询get请求
	app.get(app.envConfig.routes.customer_user_one.url,CKL.logined);
	app.get(app.envConfig.routes.customer_user_one.url,function(req,res){
		getCustomer(req,res);
	});
	
	//客服id查询post请求
	app.post(app.envConfig.routes.customer_user_one.url,CKL.logined);
	app.post(app.envConfig.routes.customer_user_one.url,function(req,res){
		getCustomer(req,res);
	});
	
	//删除
	app.get(app.envConfig.routes.customer_user_del.url,CKL.logined);
	app.get(app.envConfig.routes.customer_user_del.url,function(req,res){
		if(req.params.id){
			dao.remove(User, req.params.id, function(err,result){
				if(err){
					return Response.jsonerror(app,res,{msg:"删除失败"});
				}else{
					return Response.jsonsuccess(app,res,{msg:"删除成功"});
				}
				
			});
		}else{
			return Response.jsonerror(app,res,{msg:"删除失败,参数错误"});
		}
	});
	
	
	//查询单个客服
	function getCustomer(req,res)
	{
		var id = req.body.id;
		dao.get(User,id,function(err,result){
			if(err || !result){
				 return Response.jsonerror(app,res,{msg:"无数据"});
			}else{
				 return Response.jsonsuccess(app,res,{id:result.id,cusName:result.username,cusPwd:result.password,cusRealName:result.realname});
			}
		});
	}
	
};